<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ncduc
 * Date: 6/15/13
 * Time: 4:39 PM
 * To change this template use File | Settings | File Templates.
 */

include_once("../libs/setup.php");
include_once("../libs/helper.php");
session_start();

if (!check_user_session()) {
    header("Location: ../signin.php");
    return;
}
//function
 $smarty->assign('wwwUrl','../');
function load_personal_infor(Mysqli $mysqli, Smarty $smarty, $userId)
{
    $lstUsers = $mysqli->query("SELECT * FROM thong_tin_ca_nhan AS ttcn LEFT  JOIN chuc_danh_khoa_hoc AS cdkh ON ttcn.MaCDKH=cdkh.Ma WHERE ttcn.MaUser={$userId}; ");
    if ($lstUsers != false) {
        $result = false;
        while ($row = $lstUsers->fetch_array(MYSQL_BOTH)) {

            $result = true;
            $smarty->assign('name', $row['HoTen']);
            $smarty->assign('gender', $row['GioiTinh']);
            $smarty->assign('birthday', $row['NTNS']);
            $smarty->assign('scientific_title', $row['MaCDKH']);
            $smarty->assign('mainOrg_vi', $row['TenCoQuan_vi']);
            $smarty->assign('mainOrg_en', $row['TenCoQuan_en']);
            $smarty->assign('office_dept', $row['TenPhongBan']);
            $smarty->assign('address', $row['DiaChi']);
            $smarty->assign('city', $row['TenTinhThanh']);
            $smarty->assign('id_number', $row['CMND']);
            $smarty->assign('student_number', $row['MSSV']);
            $smarty->assign('trainee_number', $row['MaHocVien']);
            $smarty->assign('office_phone', $row['DienThoai']);
            $smarty->assign('mobile_phone', $row['DienThoaiDiDong']);
            $smarty->assign('bank_account', $row['SoTaiKhoan']);
            $smarty->assign('bank_name', $row['TenNganHang']);

     
        }
        if (!$result)
        {
            system_failed("no result", $smarty);
            return false;
        }
    }
    if ($mysqli->error)
    {
        system_failed($mysqli->error, $smarty);
        return false;
    }
    return true;
}
function get_scientific_title_list(Mysqli &$mysqli)
{
    $query = "SELECT * FROM chuc_danh_khoa_hoc";
    $result = $mysqli->query($query);
    if ($result) {
        $array_science_title = array();
        $lang = $_COOKIE['lang_vinastemcelllab'];
       
        while ($row = $result->fetch_array(MYSQL_BOTH)) {
            $array_science_title[] = array("Ma" => $row['Ma'], "TenChucDanh" => $row['TenChucDanh_'.$lang]);
        }
        return $array_science_title;
    }
    return NULL;
}

function get_city_list(Mysqli &$mysqli)
{
    $query = "SELECT * FROM tinh_thanh";
    $result = $mysqli->query($query);
    if ($result) {
        $array_city = array();
        while ($row = $result->fetch_array(MYSQL_BOTH)) {
            $array_city[] = $row['TenTinhThanh'];
        }
        return $array_city;
    }
    return NULL;
}

assign_home_header($smarty);
//get data
//Fill science titles
$mysqli = connect_database($smarty);
$arr_science_title = get_scientific_title_list($mysqli);
$smarty->assign('scientific_titles', $arr_science_title);
$arr_city = get_city_list($mysqli);
$smarty->assign('cities', $arr_city);
$mysqli->close();
if (empty($_REQUEST['save']) && empty($_REQUEST['next'])) {
    $user_id = $_SESSION['ma_user'];
    $mysqli = connect_database($smarty);
    if(!load_personal_infor($mysqli,$smarty,$user_id))
            return;
    $smarty->display('profile/1.tpl');
   
} else {


    $user_id = $_SESSION['ma_user'];
    $mysqli = connect_database($smarty);
    $name = addslashes($_REQUEST['name']);
    $gender = addslashes($_REQUEST['gender']);
    $birthday = $_REQUEST['birthday'];
    $scientific_title = $_REQUEST['scientific_title'];
    $mainOrg_vi = addslashes($_REQUEST['mainOrg_vi']);
    $mainOrg_en = addslashes($_REQUEST['mainOrg_en']);
    $office_dept = addslashes($_REQUEST['office_dept']);
    $address = addslashes($_REQUEST['address']);
    $city = addslashes($_REQUEST['city']);
    $id_number = addslashes($_REQUEST['id_number']);
    $student_number = addslashes($_REQUEST['student_number']);
    $trainee_number = addslashes($_REQUEST['trainee_number']);
    $office_phone = addslashes($_REQUEST['office_phone']);
    $mobile_phone = addslashes($_REQUEST['mobile_phone']);
    $bank_account = addslashes($_REQUEST['bank_account']);
    $bank_name = addslashes($_REQUEST['bank_name']);
    $query = "UPDATE thong_tin_ca_nhan SET HoTen='{$name}',GioiTinh={$gender},NTNS=STR_TO_DATE('{$birthday}','%d-%m-%Y')";

    if (!empty($scientific_title))
        $query .= ",MaCDKH={$scientific_title}";
    if (!empty($mainOrg_vi))
        $query .= ",TenCoQuan_vi='{$mainOrg_vi}'";
    if (!empty($mainOrg_en))
        $query .= ",TenCoQuan_en='{$mainOrg_en}'";
    if (!empty($office_dept))
        $query .= ",TenPhongBan='{$office_dept}'";
    if (!empty($address))
        $query .= ",DiaChi='{$address}'";
    if (!empty($city))
        $query .= ",TenTinhThanh='{$city}'";
    if (!empty($id_number))
        $query .= ",CMND='{$id_number}'";
    if (!empty($student_number))
        $query .= ",MSSV='{$student_number}'";
    if (!empty($trainee_number))
        $query .= ",MaHocVien='{$trainee_number}'";
    if (!empty($office_phone))
        $query .= ",DienThoai='{$office_phone}'";
    if (!empty($mobile_phone))
        $query .= ",DienThoaiDiDong='{$mobile_phone}'";
    if (!empty($bank_account))
        $query .= ",SoTaiKhoan='{$bank_account}'";
    if (!empty($bank_name))
        $query .= ",TenNganHang='{$bank_name}'";
    $query .= " where MaUser={$user_id}";
    $result = $mysqli->query($query);
    //$result = $mysqli->query("update thong_tin_ca_nhan set HoTen='{$name}', GioiTinh={$gender},NTNS={$birthday},MaCDKH={$science_title},TenCoQuan_vi='{$mainOrg_vi}',TenCoQuan_en='{$mainOrg_en}',TenPhongBan='{$office_dept}',DiaChi='{$address}',TenTinhThanh='{$city}',CMND='{$id_number}',MSSV='{$student_number}',MaHocVien='{$trainee_number}',DienThoai='{$office_phone}',DienThoaiDiDong='{$mobile_phone}',SoTaiKhoan='{$bank_account}',TenNganHang='{$bank_name}' where MaUser={$user_id}");
    if (!$result) {
        system_failed($mysqli->error . $query, $smarty);
    } else {
        if (!empty($_REQUEST['save'])) {
            if(!load_personal_infor($mysqli, $smarty, $user_id))
                    return;
            $smarty->display('profile/1.tpl');
        } else {//next button
            header("Location: 2.php");
        }
    }
}
 $mysqli->close();